<?php

namespace app\admin\controller\behavior;

use app\common\controller\Backend;

class Behavior extends Backend
{
    /**
     * @var \app\common\model\Behavior
     */
    protected $model = null;
    protected $searchFields = 'name';
    protected $noNeedRight = ['typeList', 'categoryList'];
    protected $excludeFields = [''];

    protected $typeData = [];
    protected $categoryData = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Behavior');

        $this->typeData = \app\common\model\Behavior::$typeList;
        $this->categoryData = \app\common\model\Behavior::$categoryList;

        $this->view->assign('typeData', $this->typeData);
        $this->view->assign('categoryData', $this->categoryData);
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model->where($where)->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            if ($list)
                $list = collection($list)->append(['status_text', 'type_text', 'category_text'])->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {

                $result = $this->model->validate('Behavior')->save($params);

                if ($result === false) {
                    $this->error($this->model->getError());
                }
                $this->success();
            }
            $this->error();
        }

        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {

                $result = $row->validate('Behavior')->save($params);
                if ($result === false) {
                    $this->error($row->getError());
                }
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function typeList()
    {
        $result = $this->typeData;

        $searchList = [];
        foreach ($result as $key => $value) {
            $searchList[] = ['id' => $key, 'name' => $value];
        }
        $data = ['searchlist' => $searchList];
        $this->success('', null, $data);
    }

    public function categoryList()
    {
        $result = $this->categoryData;

        $searchList = [];
        foreach ($result as $key => $value) {
            $searchList[] = ['id' => $key, 'name' => $value];
        }
        $data = ['searchlist' => $searchList];
        $this->success('', null, $data);
    }

}
